package com.foreveross.bsl.system.audit.web;

import java.util.Iterator;
import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.foreveross.bsl.common.utils.enums.AccountStatus;
import com.foreveross.bsl.common.utils.enums.Status;
import com.foreveross.bsl.common.utils.web.model.PageMustache;
import com.foreveross.bsl.system.application.AccountActivatingCheckCodeApplication;
import com.foreveross.bsl.system.application.AccountApplication;
import com.foreveross.bsl.system.application.AccountInfoApplication;
import com.foreveross.bsl.system.application.vo.AccountActivatingCheckCodeVO;
import com.foreveross.bsl.system.application.vo.AccountInfoVO;
import com.foreveross.bsl.system.application.vo.AccountVO;
import com.foreveross.bsl.web.BaseController;

/**
 * 注册审核
 * @author jake
 * @since 2014-8-12
 */
@Controller
@RequestMapping("/system/registerAudit")
public class RegisterAuditController extends BaseController{

	@Inject private AccountApplication accountApplication;
	@Inject private AccountInfoApplication accountInfoApplication;
	@Inject private AccountActivatingCheckCodeApplication accountActivatingCheckCodeApplication;
	
	@RequestMapping("/list")
	public String list(HttpServletRequest request,Model model,@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = "10") int pageSize) {
	//	Pageable pageable = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.ASC, "create_date"));
		
		//1.所有待审核的用户
		List<AccountVO> accountVoList=accountApplication.findAllByStatus(AccountStatus.Auditing.getValue());
		//2.邮箱未激活的用户(加载量少)
		List<AccountActivatingCheckCodeVO> checkcodeVoList=accountActivatingCheckCodeApplication.findAllByStatus(Status.YES.getValue());
		//3.待审核且邮箱己激活的用户才显示给管理员审核
		if(null!=checkcodeVoList && checkcodeVoList.size()>0){
			for(AccountActivatingCheckCodeVO checkCodeVo:checkcodeVoList){
				Iterator<AccountVO> itr=accountVoList.iterator();
				while(itr.hasNext()){
					if(checkCodeVo.getAccountId().equals(itr.next().getId())){
						//找到未激活邮箱的待审核用户移除
						itr.remove();
						break;
					}
				}
			}
			
		}
		
		model.addAttribute("list", PageMustache.showList(accountVoList, pageNumber-1, pageSize));
		model.addAttribute("page", new PageMustache(accountVoList.size(), pageNumber-1, pageSize));
		return "audit/audit-list";
	}
	
	@RequestMapping("/detail/{id}")
	public String detail(@PathVariable("id") String accountId,Model model) {
		AccountInfoVO accountInfo=accountInfoApplication.getEntityByAccountId(accountId);
		model.addAttribute("accountInfo", accountInfo);
		return "audit/audit-detail";
	}
	
	protected String getModuleMenuName() {
		// TODO Auto-generated method stub
		return "系统设置";
	}

}
